package dormouse.vfs.metadata;

import java.awt.BorderLayout;
import java.awt.Component;
import java.io.IOException;
import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;

import javax.swing.JPanel;

import org.apache.commons.vfs2.FileObject;
import org.apache.commons.vfs2.FileSystemException;

import dormouse.vfs.event.IFileSelectionEvent;
import dormouse.vfs.ui.renderers.FileRendererFactory;
import dormouse.vfs.ui.renderers.IFileRenderer;
import dormouse.vfs.ui.renderers.MetadataTableModel;

/**
 * This panel displays file metadata and a pluggable preview component.
 */
@SuppressWarnings("serial")
public class PreviewPanel extends JPanel implements IMetadataViewer<Component> {

	/** Creates new form PreviewPanel */
	public PreviewPanel() {
		initComponents();
		this.previewPanel.setLayout(new BorderLayout());
		// this.propertyTable = new JTable(new MetadataTableModel());
		this.propertyTable.setFillsViewportHeight(true);
	}

	@Override
	public void preview(FileObject file) {
		this.fileNameField.setText(file.getName().getBaseName());

		try {
			this.fileSizeField.setText(String.valueOf(file.getContent()
					.getSize()));
		} catch (FileSystemException ex) {
			Logger.getLogger(PreviewPanel.class.getName()).log(Level.SEVERE,
					null, ex);
		}

		IMetadataReader mdreader = MetadataReaderFactory.getInstance(file);
		if (mdreader != null) {
			try {
				Map<String, String> map = mdreader.getMetadata(file);
				MetadataTableModel model = new MetadataTableModel(map);
				this.propertyTable.setModel(model);
				this.propertyTable.updateUI();

			} catch (IOException ex) {
				Logger.getLogger(PreviewPanel.class.getName()).log(
						Level.SEVERE, null, ex);
			}

		}

		IFileRenderer renderer = getPreviewComponent(file);
		if (renderer != null) {
			this.previewPanel.removeAll();
			this.previewPanel.add((Component)renderer.getComponent(), BorderLayout.CENTER);
			this.previewPanel.validate();
			renderer.render(file);
		}
	}

	@Override
	public IFileRenderer<Component> getPreviewComponent(FileObject file) {
		return FileRendererFactory.getInstance(file);
	}
	
	@Override
	public String[] getExtensions() {
		return null;
	}

	/**
	 * This method is called from within the constructor to initialize the form.
	 * WARNING: Do NOT modify this code. The content of this method is always
	 * regenerated by the Form Editor.
	 */
	@SuppressWarnings("unchecked")
	// <editor-fold defaultstate="collapsed"
	// desc="Generated Code">//GEN-BEGIN:initComponents
	private void initComponents() {

		fileNameField = new javax.swing.JTextField();
		fileSizeField = new javax.swing.JTextField();
		jScrollPane1 = new javax.swing.JScrollPane();
		propertyTable = new javax.swing.JTable();
		previewPanel = new javax.swing.JPanel();

		setName("Form");

		org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application
				.getInstance(dormouse.Dormouse.class).getContext()
				.getResourceMap(PreviewPanel.class);
		fileNameField.setText(resourceMap.getString("fileNameField.text")); // NOI18N
		fileNameField.setName("fileNameField"); // NOI18N

		fileSizeField.setEditable(false);
		fileSizeField.setText(resourceMap.getString("fileSizeField.text")); // NOI18N
		fileSizeField.setName("fileSizeField"); // NOI18N

		jScrollPane1.setName("jScrollPane1"); // NOI18N

		propertyTable.setModel(new MetadataTableModel());
		propertyTable.setName("propertyTable"); // NOI18N

		jScrollPane1.setViewportView(propertyTable);

		previewPanel.setBorder(javax.swing.BorderFactory
				.createTitledBorder(resourceMap
						.getString("previewPanel.border.title"))); // NOI18N
		previewPanel.setName("previewPanel"); // NOI18N

		javax.swing.GroupLayout previewPanelLayout = new javax.swing.GroupLayout(
				previewPanel);
		previewPanel.setLayout(previewPanelLayout);
		previewPanelLayout.setHorizontalGroup(previewPanelLayout
				.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
				.addGap(0, 208, Short.MAX_VALUE));
		previewPanelLayout.setVerticalGroup(previewPanelLayout
				.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
				.addGap(0, 127, Short.MAX_VALUE));

		javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
		this.setLayout(layout);
		layout.setHorizontalGroup(layout
				.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
				.addGroup(
						javax.swing.GroupLayout.Alignment.TRAILING,
						layout.createSequentialGroup()
								.addContainerGap()
								.addGroup(
										layout.createParallelGroup(
												javax.swing.GroupLayout.Alignment.TRAILING)
												.addComponent(
														previewPanel,
														javax.swing.GroupLayout.Alignment.LEADING,
														javax.swing.GroupLayout.DEFAULT_SIZE,
														javax.swing.GroupLayout.DEFAULT_SIZE,
														Short.MAX_VALUE)
												.addComponent(jScrollPane1, 0,
														0, Short.MAX_VALUE)
												.addComponent(
														fileSizeField,
														javax.swing.GroupLayout.Alignment.LEADING,
														javax.swing.GroupLayout.DEFAULT_SIZE,
														220, Short.MAX_VALUE)
												.addComponent(
														fileNameField,
														javax.swing.GroupLayout.DEFAULT_SIZE,
														220, Short.MAX_VALUE))
								.addContainerGap()));
		layout.setVerticalGroup(layout
				.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
				.addGroup(
						javax.swing.GroupLayout.Alignment.TRAILING,
						layout.createSequentialGroup()
								.addContainerGap()
								.addComponent(previewPanel,
										javax.swing.GroupLayout.DEFAULT_SIZE,
										javax.swing.GroupLayout.DEFAULT_SIZE,
										Short.MAX_VALUE)
								.addPreferredGap(
										javax.swing.LayoutStyle.ComponentPlacement.RELATED)
								.addComponent(fileNameField,
										javax.swing.GroupLayout.PREFERRED_SIZE,
										javax.swing.GroupLayout.DEFAULT_SIZE,
										javax.swing.GroupLayout.PREFERRED_SIZE)
								.addPreferredGap(
										javax.swing.LayoutStyle.ComponentPlacement.RELATED)
								.addComponent(fileSizeField,
										javax.swing.GroupLayout.PREFERRED_SIZE,
										javax.swing.GroupLayout.DEFAULT_SIZE,
										javax.swing.GroupLayout.PREFERRED_SIZE)
								.addPreferredGap(
										javax.swing.LayoutStyle.ComponentPlacement.RELATED)
								.addComponent(jScrollPane1,
										javax.swing.GroupLayout.PREFERRED_SIZE,
										241,
										javax.swing.GroupLayout.PREFERRED_SIZE)
								.addContainerGap()));
	}// </editor-fold>//GEN-END:initComponents

	// Variables declaration - do not modify//GEN-BEGIN:variables
	private javax.swing.JTextField fileNameField;
	private javax.swing.JTextField fileSizeField;
	private javax.swing.JScrollPane jScrollPane1;
	private javax.swing.JPanel previewPanel;
	private javax.swing.JTable propertyTable;
	// End of variables declaration//GEN-END:variables
	
	@Override
	public Component getComponent() {
		return this;
	}

	@Override
	public void fileSelected(IFileSelectionEvent event) {
		FileObject file = event.getSelectedFile();
		preview(file);	
	}

}
